/*
 * Class: Test
 * 
 * Created on Sep 26, 2013
 * 
 * (c) Copyright by Minh Tran
 * Global Cybersoft VN.
 * Floor 3-4-5, Helios Building, Quang Trung Software City
 */
package com.minh.tran.sg;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.jsoup.Jsoup;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Test
{
    public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException
    {
        String link = "http://www.nhaccuatui.com/playlist/o-noi-do-em-cuoi-dan-truong.J2WgkbhlTEZ0.html";

        String htmlSource = Jsoup.connect(link).get().html();

        System.out.println();
        String file = "file=http://www.nhaccuatui.com/flash/xml?key";
        int first = htmlSource.indexOf(file);
        int end = htmlSource.indexOf("\"", first);
        file = htmlSource.substring(first + 5, end);
        System.out.println(file);

        String htmlSource1 = Jsoup.connect(file).get().html();
        System.out.println(htmlSource1);

        //==========
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(file);

        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("track");
        for (int temp = 0; temp < nList.getLength(); temp++)
        {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE)
            {

                Element eElement = (Element) nNode;

                System.out.println("title : " + eElement.getElementsByTagName("title").item(0).getTextContent());
                System.out.println("creator : " + eElement.getElementsByTagName("creator").item(0).getTextContent());
                System.out.println("location : " + eElement.getElementsByTagName("location").item(0).getTextContent());
                System.out.println("info : " + eElement.getElementsByTagName("info").item(0).getTextContent());
                System.out.println("image : " + eElement.getElementsByTagName("image").item(0).getTextContent());

            }
        }
    }
}
